<?php
header("Content-type:text/html;charset = utf-8");
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Headers:*');
//header('Access-Control-Request-Method:GET,POST');
function makeMD5($data)
{
    $data = md5($data) . 'laqn';
    return md5($data);
}

function TakeData($key_list = [], $is_safe = false)
{
    $data = ( array )json_decode(json_encode($_REQUEST), true);
    $return_data = [];
    foreach ($key_list as $key => $value) {
        if ($is_safe) {
            if ($data[$value] == "" || $data[$value] == null) {
                make_json_result(0, $value . "传入参数错误");
            }
        }
        if (isset($data[$value])) {
            $data[$value] = trim($data[$value]);
            $data[$value] = strip_tags($data[$value]);
            $data[$value] = htmlspecialchars($data[$value]);
            $data[$value] = addslashes($data[$value]);
            $return_data[$value] = $data[$value];
        }
    }
    return $return_data;
}


//$data表示TakeData过来的数组，下同
function AddInDB($DBName, $data)
{
    $DB = D('' . $DBName . '');
    return $DB->add($data,[],true);
}

function FindInDB($DBName, $data, $field = null)
{
    $DB = D('' . $DBName . '');
    if ($field) {
        return $DB->$field($field)->where($data)->find();
    } else {
        return $DB->where($data)->find();
    }
}

function SaveInDB($DBName, $data)
{
    $DB = D('' . $DBName . '');
    return $DB->save($data);
}

function CreateInDB($DBName, $data)
{
    $User = M('' . $DBName . '');
    // 创建数据后写入到数据库
    $User->data($data)->add();
}

function DeleteInDB($DBName, $data)
{
    $DB = D('' . $DBName . '');
    return $DB->where($data)->delete();
}

function SelectInDB($DBName, $data = null)
{
    $DB = D('' . $DBName . '');
    if ($data != NULL) {
        return $DB->where($data)->select();
    } else {
        return $DB->select();
    }
}

function SQLInDB($sql)
{
    $sql = str_replace('	', ' ', $sql);
    $sqltype = (explode(' ', $sql));
    $sqltype = $sqltype[0];
    if (strcasecmp($sqltype, "select") == 0) {
        $DB = M();
        return $DB->query($sql);
    } else {
        $DB = M();
        return $DB->execute($sql);
    }

}

function SavaInDB_S($DBName, $data)
{
    $DB = D('' . $DBName . '');
    $DB->startTrans();
    $result = $DB->save($data);
    if ($result || $result == 0) {
        $DB->commit();//成功则提交
        return 1;
    } else {
        $DB->rollback();//不成功，则回滚
        return 0;
    }
}

function DeleteInDB_S($DBName, $DBkey, $DBval)
{
    $DB = D('' . $DBName . '');
    $DB->startTrans();
    $result = $DB->where('' . $DBkey . '=' . "'" . $DBval . "'" . '')->delete();
    if ($result || $result == 0) {
        $DB->commit();//成功则提交
        return 1;
    } else {
        $DB->rollback();//不成功，则回滚
        return 0;
    }
}

function AddInDB_S($DBName, $data)
{
    $DB = D('' . $DBName . '');
    $DB->startTrans();
    $result = $DB->add($data);
    if ($result || $result == 0) {
        $DB->commit();//成功则提交
        return 1;
    } else {
        $DB->rollback();//不成功，则回滚
        return 0;
    }
}

function make_json_result($status, $msg, $res = array())
{
    $result['status'] = $status;
    $result['result'] = $res;
    $result['msg'] = $msg;
    echo json_encode($result);
    exit;
}

function SendIdeCode($to)
{
    // vendor('PHPMailer.class#PHPMailer');
    Vendor('PHPMailer.PHPMailerAutoload');
    vendor('phpmailer.class#phpmailer');
    $idecode = GetRandStr(4);
    $mail = new PHPMailer();
    // 装配邮件服务器

    // $mail->SMTPDebug = 3;
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.qq.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '3361990686@qq.com';                 // SMTP username
    $mail->Password = 'grkrdcemmyqfcjhb';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to
    $mail->setFrom('3361990686@qq.com', 'TopV管理员');
    $mail->addAddress($to, 'Joe User');     // Add a recipient
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'TopV账号注册验证码';
    $mail->Body = '你的TopV账号注册验证码为【' . $idecode . '】，5分钟内有效，请你尽快输入，务必不能告知其他人';
    // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    if ($mail->send()) {
        return $idecode;
    } else {
        return null;
    }
}

function GetRandStr($len)
{
    $chars = array(
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
        "3", "4", "5", "6", "7", "8", "9"
    );
    $charsLen = count($chars) - 1;
    shuffle($chars);
    $output = "";
    for ($i = 0; $i < $len; $i++) {
        $output .= $chars[mt_rand(0, $charsLen)];
    }
    return $output;
} 